package com.tyx.chargingpile.service.impl;

import com.tyx.chargingpile.Enum.ServiceMessageEume;
import com.tyx.chargingpile.cache.SocketChannelMap;
import com.tyx.chargingpile.service.ChargingPileService;
import com.tyx.chargingpile.service.MessageService;
import com.tyx.chargingpile.util.Util;
import com.tyx.chargingpile.vo.SocketResponseResult;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * vin鉴权结果返回
 */
@Service("chargingPileVinCodeCheckResultDown")
public class ChargingPileVinCodeCheckResultDown implements ChargingPileService {

    private final static Logger LOGGER = LoggerFactory.getLogger(ChargingPileVinCodeCheckResultDown.class);

    @Override
    public SocketResponseResult execute(ChannelHandlerContext channelHandlerContext, byte[] context) throws Exception {
        return null;
    }

    @Override
    public SocketResponseResult execute(ChannelHandlerContext channelHandlerContext, Map data) throws Exception {
        String pileNum = (String) data.get("PileId");
        String gunpointNum = (String) data.get("ChargePortIndex");
        String vin = (String) data.get("VIN");
        String amountMoney = (String) data.get("AmountMoney");
        String authResult = (String) data.get("AuthResult");
        String errorCode = (String) data.get("ErrorCode");
        String powerType = SocketChannelMap.getPileInfo(pileNum).getPowerType();
        // 数据往前补0
        gunpointNum = Util.FronFillZero(gunpointNum,2);
        pileNum = Util.FronFillZero(pileNum,12);
        // 把amountMoney转化为分的单位，补够6位数
        amountMoney = Util.formatDoubleTOInt(Double.parseDouble(amountMoney),100) + "";
        amountMoney = Util.FronFillZero(amountMoney,8);
//        authResult = Util.FronFillZero(authResult,2);
        errorCode = Util.FronFillZero(errorCode,2);

        Map<String,String> dwn = new HashMap<>();
        dwn.put("gunpointNum",gunpointNum);
        dwn.put("powerType",powerType);
        dwn.put("pileNum",pileNum);
        dwn.put("vinCode",vin);
        dwn.put("balacnce",amountMoney);
        dwn.put("status",errorCode);
        if (authResult.equals("1")){
            dwn.put("result","FF");
        }else {
            dwn.put("result","00");
        }
        byte[] dwnContext = MessageService.getMessage(dwn, ServiceMessageEume.VINCODECHECKRESULT.getDwnLinePath());
        ByteBuf pingMessage = Unpooled.buffer();
        pingMessage.writeBytes(dwnContext);
        channelHandlerContext.writeAndFlush(pingMessage);
        String commandId = ServiceMessageEume.VINCODECHECKRESULT.getCode();
        LOGGER.info("客户端"+channelHandlerContext.channel().id()+",指令控制编号："+commandId+",桩编号:" + pileNum + ",vin鉴权结果返回下行报文" + Util.BCD2String(dwnContext));
        return new SocketResponseResult(channelHandlerContext,dwnContext);
    }
}
